/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.persistence.c14n;

import org.proteusframework.platformservice.persistence.messagebean.DataType;

/**
 * Canonicalization (C14N) factory produces a persistence service agnostic representation of extended datatypes. The
 * complete list of extended data types include each of the {@link DataType} <code*ArrayType</code> and the
 * {@link DataType#SerializableType}.
 * <p/>
 * Custom persistence service providers <i>may</i> opt to utilize this canonicalization for their custom
 * implementation of {@link org.proteusframework.platformservice.persistence.api.IMessageBeanManager}. Custom
 * persistence service providers with {@link org.proteusframework.platformservice.persistence.api.IPackageManager}
 * support <i>must</i> utilize <code>C14NFactory</code> to ensure that these custom types are handled in an
 * agnostic manner.
 *
 * @author Tacoma Four
 */
public final class C14NFactory
{
    private C14NFactory()
    {
    }

    /**
     * Instantiates a new {@link IC14NHandler} based on the specified {@link DataType}.
     *
     * @param dataType Data type
     * @return Valid C14N handler
     * @throws IllegalArgumentException when the data type provided is not either a {@link DataType}
     *                                  <code>*ArrayType</code> or {@link DataType#SerializableType}.
     */
    public static IC14NHandler getHandler(DataType dataType)
    {
        IC14NHandler handler = null;

        switch (dataType)
        {
            case SerializableType:
                handler = new SerializationC14NHandler();
                break;
            case BooleanArrayType:
                handler = new BooleanArrayC14NHandler();
                break;
            case StringArrayType:
                handler = new StringArrayC14NHandler();
                break;
            case IntegerArrayType:
                handler = new IntegerArrayC14NHandler();
                break;
            case LongArrayType:
                handler = new LongArrayC14NHandler();
                break;
            case ShortArrayType:
                handler = new ShortArrayC14NHandler();
                break;
            case FloatArrayType:
                handler = new FloatArrayC14NHandler();
                break;
            case DoubleArrayType:
                handler = new DoubleArrayC14NHandler();
                break;
            case DateArrayType:
                handler = new DateArrayC14NHandler();
                break;
            case NamespaceArrayType:
                handler = new NamespaceArrayC14NHandler();
                break;
            default:
                throw new IllegalArgumentException("Data type " + dataType.toString() + " is not a binary data type and does not require canonicalization");
        }

        return handler;
    }
}
